
template <uint blockSize>
__global__ void reduceRemainingParameters(AxisOrientation_t* d_in, int size)	
{
	extern __shared__ int sdata[];

	uint tid      = threadIdx.x;
	uint i        = blockIdx.x * (blockSize * 2) + tid;
	uint gridSize = blockSize * 2 * gridDim.x;

//	prinitf("thread: %d\t i: %d\t gridSize: %d\t blockIdx.x: %d\t gridDim.x:n %d\t blockSize: %d\t size: %d\n", tid, i, gridSize, blockIdx.x, gridDim.x, blockSize, size);
	sdata[tid] = i; i+=gridSize;

//	for(int k = 0; k < 512; k++)
//		printf("sdata[%d]: %d\n", tid, sdata[tid]);

	while (i < size) {
		if (d_in[sdata[tid]].votes < d_in[i].votes)
			sdata[tid] = i;
		i+=gridSize;
	}


//		printf("sdata[%d]: %d\n", tid, sdata[tid]);
	__syncthreads();

	if (blockSize >= 512) {
		if (tid < 256) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 256]].votes)
				sdata[tid] = sdata[tid + 256];
		}
	}
	__syncthreads();

	if (blockSize >= 256) {
		if (tid < 128) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 128]].votes)
				sdata[tid] = sdata[tid + 128];
		}
	}
	__syncthreads();

	if (blockSize >= 128) {
		if (tid < 64) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 64]].votes)
				sdata[tid] = sdata[tid + 64];
		}
	}
	__syncthreads();

	if (tid < 32) {
		if (blockSize >= 64) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 32]].votes)
				sdata[tid] = sdata[tid + 32];
		}
		if (blockSize >= 32) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 16]].votes)
				sdata[tid] = sdata[tid + 16];
		}
		if (blockSize >= 16) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 8]].votes)
				sdata[tid] = sdata[tid + 8];
		}
		if (blockSize >= 8) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 4]].votes)
				sdata[tid] = sdata[tid + 4];
		}
		if (blockSize >= 4) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 2]].votes)
				sdata[tid] = sdata[tid + 2];
		}
		if (blockSize >= 2) {
			if (d_in[sdata[tid]].votes < d_in[sdata[tid + 1]].votes)
				sdata[tid] = sdata[tid + 1];
		}
	}

	if (tid == 0) 
	{
		d_in[blockIdx.x].a  		= d_in[sdata[0]].a;
		d_in[blockIdx.x].b  		= d_in[sdata[0]].b;
		d_in[blockIdx.x].c  		= d_in[sdata[0]].c;
		d_in[blockIdx.x].alpha  	= d_in[sdata[0]].alpha;
		d_in[blockIdx.x].beta   	= d_in[sdata[0]].beta;
		d_in[blockIdx.x].theta  	= d_in[sdata[0]].theta;
		d_in[blockIdx.x].centroid  	= d_in[sdata[0]].centroid;
		d_in[blockIdx.x].votes  	= d_in[sdata[0]].votes;

	}
}
